﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Text;

namespace Jinjia.Approve.Common.Extensions
{
    /// <summary>
    /// Json帮助扩展方法
    /// </summary>
    public static class JsonExtension
    {
        /// <summary>
        /// 实体转Json
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="input"></param>
        /// <returns></returns>
        public static string ToJson<T>(this T input) 
        {
            if (input == null)
                return null;

            return JsonConvert.SerializeObject(input);
        }

        /// <summary>
        /// 实体转Json
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="input"></param>
        /// <returns></returns>
        public static R ToObject<R>(this string input) where R : class
        {
            if (string.IsNullOrWhiteSpace(input))
                return default;

            return JsonConvert.DeserializeObject<R>(input);
        }
    }
}
